define(function(require,exports,module) {
    var myPub = require('./public');
    var payPub = require('./submit_pay');
    var $orderForm = $('#orderForm');           // 需要支付的表单
    // 礼金卡支付密码
    var oCashMain = $('.js_cashMain');                          // 礼金卡弹出区域
    var oSxcashCardArea = $('.js_sxcashCardArea_input');        // 礼金卡密码支付的点击区域
    var oPsdEntry = $('.js_psdEntry');                          // 礼金卡密码的input框
    var oPsdAreaDiv = $('.js_psdArea div');                     // 礼金卡的显示区域
    var oPsdAreaTip = $('.js_sxcashCardArea_Tip');              // 礼金卡的输入提示
    var oShadows = $('.js_shadowlpk');
    var oCloseBtn = $('.js_closeBtn');
    var paymentId=$("#paymentId");
    var tempOrderSn=$("#tempOrderSn");
    var tempOrderType=$("#tempOrderType");
    var gfcUserPhone=$("#gfcUserPhone");
    var nCashPsdIndex = 0;                                       // 输入的长度
    var kcuid =$("#kcuid").val();//为爱狂吃活动？？？
    var psdScrollTap_timer = null;                              // 失去焦点页面滚动定时器

    //加密
    var encrypt = function Encrypt(word) {
        var strInfo = "0102030405060708";
        var key = CryptoJS.enc.Utf8.parse(strInfo);
        var iv = CryptoJS.enc.Utf8.parse(strInfo);
        var srcs = CryptoJS.enc.Utf8.parse(word);
        var encrypted = CryptoJS.AES.encrypt(srcs, key, {iv: iv, mode: CryptoJS.mode.CBC});
        return encrypted.toString();
    }

    oSxcashCardArea.on('click',function () {
        oPsdEntry.focus();
        oPsdAreaDiv.eq(nCashPsdIndex).html('<em></em>');
    });
    oPsdEntry.on('input propertychange',function () {
        oPsdAreaDiv.html('');
        nCashPsdIndex = oPsdEntry.val().length;
        for(var i = 0;i<oPsdEntry.val().length;i++){
            oPsdAreaDiv.eq(i).html('<i></i>');
            oPsdAreaDiv.eq(i+1).html('<em></em>');
        }

        if(oPsdEntry.val().length == 0){
            oPsdAreaTip.text('');
            oPsdAreaDiv.eq(0).html('<em></em>');
        }else if(oPsdEntry.val().length == 6){
            submitLjkPay();
            //toPay();


        }
    });
    oPsdEntry.on('blur',function () {
        clearTimeout(psdScrollTap_timer);
        psdScrollTap_timer = setTimeout(function () {
            window.scrollTo(0,document.documentElement.clientHeight);
        },100);
        if(nCashPsdIndex == 0){
            oPsdAreaDiv.eq(0).html('');
        }else{
            oPsdAreaDiv.eq(nCashPsdIndex).html('');
        }
    });
    var cashHeight = oCashMain.outerHeight(true);
    oCashMain.css({'bottom': -cashHeight });
    // 礼金卡显示
   /* function ljkPsdShow() {
        oCashMain.show().stop().animate({ 'bottom':0 },300,function() {
            oShadows.show();
        });
        myPub.HamstopPropaga();
    }
    exports.ljkPsdShow = ljkPsdShow;*/
    // 礼金卡关闭按钮
    oCloseBtn.on('click',function(e) {
        oCashMain.stop().animate({bottom:-cashHeight},300,function(){
            oCashMain.hide();
            oShadows.hide();
            payCancel();
        });
        myPub.HamstopPropaga()
    });
    // 礼金卡点击阴影关闭按钮
    oShadows.on('click',function () {
        oCashMain.stop().animate({bottom:-cashHeight},300,function(){
            oCashMain.hide();
            oShadows.hide(); payCancel();
        });
    })

    // 如果密码错误则失去焦点清空密码
    function deleteCode() {
        oPsdEntry.val('');
        oPsdAreaDiv.html('');
        nCashPsdIndex = 0;
        oPsdEntry.blur();
    }
    
    function submitLjkPay(){
        var gfcPayPassword = encrypt(oPsdEntry.val());
        G_JSON_POST("/prepaidCard/checkPayPassword",{phone:gfcUserPhone.val(),payPassword:gfcPayPassword},function (data) {
            var code =data.code;
            if(1===code){
                oCashMain.stop().animate({bottom:-cashHeight},1000,function(){
                    oCashMain.hide();
                    oShadows.hide();
                });
                myPub.HamstopPropaga();
                $orderForm.find('input[name="cardUserPassword"]').val(gfcPayPassword);
                $orderForm.find('input[name="prepaidCard"]').val(true);
                toPay();
                // paySuccess();
            }else{
                deleteCode();
                 myPub.TipLayer(data.msg,'no',function () {
                  },3);
            }
        });
    }

    /**
     * 提交支付的方法
     */
    function toPay() {
        if( isGrouperFree == "true" ){
            var payUrl = "/shop/nowbuy/groupFreePay";
        }else{
            var payUrl = "/shop/payment/orderPay";
        }
        var data = $orderForm.serialize();
        payPub.submitPay(payUrl,data);
    }

    function payCancel(){
        // TipLayer(res.errMsg);
        // localStorage.setItem("orderNav_index",1);
        // window.location.href = "/shop/pay/order_list";
    }

    function paySuccess(){
        if(tempOrderType.val()!= null && (tempOrderType.val() == "GIFT"||tempOrderType.val() == "MULTIGIFT")){
            myPub.locationHref("/shop/gift/sharePage/" + paymentId.val());
        }
        var isGroupBuy = $orderForm.find('input[name="isGroupBuy"]').val();
        if("yes" == isGroupBuy){
            G_JSON_POST("/GroupBuying/getOrderInfo","orderSn="+tempOrderSn.val(),function (data) {
                var recordId= data.recordId
                if(typeof recordId != 'undefined') {
                    myPub.locationHref("/h/tuangou/groupOrderDetail/" + recordId);
                }
            })
        } else {
            var _orderSn_kcuid = '';
            if(kcuid){
                _orderSn_kcuid = "?orderSn=" + tempOrderSn.val() + "&payId=" + paymentId + "&kcuid=" + kcuid;
            }
            else{
                _orderSn_kcuid = "?orderSn=" + tempOrderSn.val() + "&payId=" + paymentId;
            }

            // myPub.locationHref("/member/order/tradeSuccess" + _orderSn_kcuid);
            myPub.locationHref("/h/goodspay/paymentOver" + _orderSn_kcuid);


        }

    }
})